#include "RenderingWidget.h"

#include <QTimer>

#include <iostream>

using namespace Gui;

RenderingWidget::RenderingWidget(QWidget *parent) : QGLWidget(parent)
{
}

void RenderingWidget::initializeGL()
{
    std::cout << "Initializing OpenGL : ";

    glClearColor( 0.1f, 0.1f, 0.1f, 0.0f );
    glViewport( 0, 0, width(), height() );

    glEnable(GL_BLEND);                                 // Turn blending On
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	// (Type of blending) Set the blending function for translucency

    glEnable( GL_DEPTH_TEST );							// Enables Depth Testing
    glEnable( GL_TEXTURE_2D );							// Enable 2D Textures
    glDisable( GL_CULL_FACE );							// Disables Cullback facing

    glShadeModel( GL_SMOOTH );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    glOrtho(-5., 5.,-5., 5.,-5., 5.);

    glMatrixMode( GL_MODELVIEW );

    std::cout << "Done" << std::endl;

    QTimer * timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(updateGL()));
    timer->start(0);
}

void RenderingWidget::resizeGL(int w, int h)
{
    glViewport( 0, 0, w, h );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    glOrtho(-5., 5.,-5., 5.,-5., 5.);

    glMatrixMode( GL_MODELVIEW );
}

void RenderingWidget::paintGL()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );


    glLoadIdentity();
    glPushMatrix();
    {
        glRotated(45.,1.,0.,0.);
        glBegin(GL_LINES);
            // X axis
            glColor3d(1.0,0.0,0.0);
            glVertex3d(0.0,0.0,0.0);
            glVertex3d(1.0,0.0,0.0);
            // Y axis
            glColor3d(0.0,1.0,0.0);
            glVertex3d(0.0,0.0,0.0);
            glVertex3d(0.0,1.0,0.0);
            // Z axis
            glColor3d(0.0,0.0,1.0);
            glVertex3d(0.0,0.0,0.0);
            glVertex3d(0.0,0.0,1.0);
        glEnd();
        glColor3d(1.0,1.0,1.0);
    }
    glPopMatrix();
}
